package com.heima.controller;

import com.heima.pojo.dto.PartnerDto;
import com.heima.pojo.vo.PartnerVo;
import com.heima.service.PartnerService;
import com.heima.utils.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author 赵超
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user-service/partner")
public class PartnerController {

    private final PartnerService partnerService;

    @GetMapping("/search")
    public PageResult<PartnerVo> page(
            @RequestParam Integer pageIndex,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name
    ) {
        return partnerService.page(pageIndex, pageSize, name);
    }

    @PostMapping
    public Boolean add(@RequestBody PartnerDto partnerDto) {
        return partnerService.add(partnerDto) > 0;
    }

    @GetMapping("/{id}")
    public PartnerVo getPartnerById(@PathVariable Integer id) {
        return partnerService.getPartnerById(id);
    }

    @PutMapping("/{id}")
    public Boolean updatePartnerById(@PathVariable Integer id, @RequestBody PartnerDto partnerDto) {
        return partnerService.updatePartnerById(id, partnerDto) > 0;
    }

    @DeleteMapping("/{id}")
    public Boolean deleteById(@PathVariable Integer id) {
        return partnerService.deleteById(id) > 0;
    }

    @GetMapping("/count")
    public Integer getPartnerCount() {
        return partnerService.count();
    }
}
